package org.ethan.pet.controller;

import org.ethan.basic.exception.BasicException;
import org.ethan.basic.util.AjaxResult;
import org.ethan.basic.util.LoginContext;
import org.ethan.basic.util.PageList;
import org.ethan.pet.domain.Pet;
import org.ethan.pet.domain.SearchMasterMsg;
import org.ethan.pet.query.SearchMasterMsgQuery;
import org.ethan.pet.service.ISearchMasterMsgService;
import org.ethan.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 发布寻主信息
 */
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService service;


    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return service.getAll();
    }


    /**
     * 发布领养信息
     * HttpServletRequest通过请求头获得token，再用token从redis中获得当前用户的登录信息
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg smm, HttpServletRequest request) {
        try {
            // 获得当前登录用户信息
            LoginInfo loginInfo = LoginContext.getLogin(request);
            service.publish(smm, loginInfo.getId());
            return AjaxResult.me();
        } catch (BasicException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统异常！！！");
        }

    }

    /**
     * 待处理的用户发布的寻主信息
     * @param query
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        try {
            LoginInfo loginInfo = LoginContext.getLogin(request);
            return service.pending(query, loginInfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 已处理的用户发布的寻主信息
     * @param query
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        try {
            LoginInfo loginInfo = LoginContext.getLogin(request);
            return service.processed(query, loginInfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 处理待处理的消息
     * @return
     */
    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            LoginInfo loginInfo = LoginContext.getLogin(request);
            service.handle(pet,loginInfo.getId());
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统异常。。。");
        }

    }


}
